#include "myhead.h"

int tcpsock;//套接字标识符
int main()
{
    tcpsock=socket(AF_INET,SOCK_STREAM,0);
    if(tcpsock==-1)
    {
        perror("创建套接字失败\n");
        return -1;
    }

    int opt = 1;
    //设置端口取消绑定限制
    setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    //设置ip和端口（不要小于1024)
    struct sockaddr_in bindaddr;//结构体里面要有ip和端口
    bindaddr.sin_family=AF_INET;//选择地址协议ipv4
    bindaddr.sin_addr.s_addr=INADDR_ANY;//ip
    bindaddr.sin_port=htons(17640);//端口

    //绑定套接字ip和端口
    int ret = bind(tcpsock,(struct sockaddr *)&bindaddr,sizeof(struct sockaddr_in));
    if(ret==-1)
    {
        perror("bind");
        return -1;
    }

    //获取服务器ip
    struct hostent *result=gethostbyname("slypass3.market.alicloudapi.com");

    //设置要连接的ip和端口
    struct sockaddr_in conaddr;//结构体里面要有ip和端口
    conaddr.sin_family=PF_INET;//选择地址协议ipv4
    conaddr.sin_addr.s_addr = ((struct in_addr *)result->h_addr_list[0])->s_addr;//ip就放在struct in_addr *(char *)

    conaddr.sin_port=htons(80);//80/443(https)
    ret = connect(tcpsock,(struct sockaddr *)&conaddr,sizeof(struct sockaddr_in));
    if(ret == -1)
    {
        perror("connect\n");
        return -1;
    }

    //获取http请求，接通api
    char *httpreq="GET /express3?number=YT8796172625031 HTTP/1.1\r\n"
                    "Host:slypass3.market.alicloudapi.com\r\n"
                    "Authorization: APPCODE e9a6ae11b6cd41af9c0a7637feb33fff\r\n\r\n";

    send(tcpsock,httpreq,strlen(httpreq),0);

    char buf[7000]="";
    printf("受到的信息是：\n");
    recv(tcpsock,buf,sizeof(buf)-1,0);
    printf("%s\n",buf);

    YT_cJSON(buf);
    close(tcpsock);
    exit(0);
}